package com.foreknow.filter;

import javax.servlet.*;
import java.io.IOException;

public class CharacterEncodingFilter implements Filter {
    private String encoding  = "ISO8859-1";
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        encoding =  filterConfig.getInitParameter("encoding");
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        servletRequest.setCharacterEncoding(encoding);
        servletResponse.setContentType("text/html;charset="+encoding);
        //如果还有下一个过滤器可以通过方法来交给下一个过滤器来处理，如果没有其它过滤器就将资源交给servlet
        filterChain.doFilter(servletRequest,servletResponse);
    }

    @Override
    public void destroy() {

    }
}
